package com.cxhc.demo1005.View;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TimeUtils;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.Nullable;

import java.util.Calendar;
import java.util.Date;

public class ClockView extends View {

    private Context mContext;
    private Paint mPaintBg;//背景绘制画笔
    private Paint mPaint;//时钟元素画笔
    private Paint mPaintLine;//指针绘制画笔
    private Paint mPaintText;//绘制数字画笔

    public ClockView(Context context) {
        this(context, null);
    }

    public ClockView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ClockView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public ClockView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        mContext = getContext();
        mPaintBg = new Paint();
        mPaintBg.setAntiAlias(true);
        mPaintBg.setColor(Color.BLACK);
        mPaintBg.setStrokeWidth(dip2px(mContext, 8));
        mPaintBg.setStyle(Paint.Style.STROKE);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.RED);
//        mPaint.setStrokeWidth(dip2px(mContext,40));
        mPaint.setStyle(Paint.Style.FILL);

        mPaintLine = new Paint();
        mPaintLine.setAntiAlias(true);
        mPaintLine.setStrokeWidth(10);
        mPaintLine.setStyle(Paint.Style.STROKE);

        mPaintText = new Paint();
        mPaintText.setAntiAlias(true);
        mPaintText.setStyle(Paint.Style.STROKE);
        mPaintText.setColor(Color.BLACK);
        mPaintText.setTextSize(30);
        start();
    }


    private float mDistanceHour;//指针长度——时针
    private float mDistanceMinute;//指针长度--分针
    private float mDistanceSecond;//指针长度--秒针

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mDistanceHour = w / 5 - dip2px(mContext, 7);
        mDistanceMinute = mDistanceHour + dip2px(mContext, 18);
        mDistanceSecond = mDistanceHour + dip2px(mContext, 45);
    }

    private int mCurrentHour = 0; //0-11
    private int mCurrentMinute = 0;//0-59
    private int mCurrentSecond = 0; //0-59

    private void drawHour(Canvas canvas) {
        float hour = (float)mCurrentHour +(float)(mCurrentMinute/60)+(float)(mCurrentSecond/3600);
        float deg = 30*hour;
        float x = (float) (mDistanceHour*Math.sin(Math.PI/180*deg));
        float y = (float) (mDistanceHour*Math.cos(Math.PI/180*deg));
        mPaintLine.setStrokeWidth(12);
        canvas.drawLine(0,0,x,-y,mPaintLine);
    }

    private void drawMinute(Canvas canvas) {
        float minute = mCurrentMinute+mCurrentSecond/60;
        float deg = 6*minute;
        float x = (float) (mDistanceMinute*Math.sin(Math.PI/180*deg));
        float y = (float) (mDistanceMinute*Math.cos(Math.PI/180*deg));
        mPaintLine.setStrokeWidth(8);
        canvas.drawLine(0,0,x,-y,mPaintLine);
    }

    private void drawSecond(Canvas canvas) {
        float second = mCurrentSecond;
        float deg = 6*second;
        float x = (float) (mDistanceSecond*Math.sin(Math.PI/180*deg));
        float y = (float) (mDistanceSecond*Math.cos(Math.PI/180*deg));
        mPaintLine.setStrokeWidth(4);
        canvas.drawLine(0,0,x,-y,mPaintLine);
    }

    private int offset = 30;
    private void drawBasic(Canvas canvas){
        canvas.drawCircle(0,0,(getWidth()-dip2px(mContext,2))/2,mPaintBg);
        canvas.drawCircle(0,0,dip2px(mContext,10),mPaint);
        for (int i = 0; i < 12; i++) {
            float radius = getWidth()/2-offset;
            float deg = 30*i+30;
            float x = (float) (radius*Math.sin(Math.PI/180*deg));
            float y = (float) (radius*Math.cos(Math.PI/180*deg));
            canvas.drawCircle(x,-y,dip2px(mContext,4),mPaint);

            String timeText = (i+1)+"";
            float textWidth = mPaintText.measureText(timeText,0,timeText.length());

            canvas.drawText(timeText,x-4,y-4,mPaintText);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(getWidth()/2,getHeight()/2);
        drawBasic(canvas);
        drawHour(canvas);
        drawMinute(canvas);
        drawSecond(canvas);
        drawTextTime(canvas);

    }

    private void drawTextTime(Canvas canvas) {
        String time = mCurrentHour +":"+mCurrentMinute+":"+mCurrentSecond;
        canvas.drawText(time,200,-30,mPaintText);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    private long Unit = 1000L;//一秒钟绘制一次

    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            handler.postDelayed(this, Unit);
            getCurrentTime();
            invalidate();
        }
    };

    public static final String TAG = ClockView.class.getSimpleName();
    private void getCurrentTime() {
        Calendar calendar = Calendar.getInstance();
        Date time = calendar.getTime();
         mCurrentHour = time.getHours()>12?time.getHours()-12:time.getHours();
         mCurrentMinute = time.getMinutes();
         mCurrentSecond = time.getSeconds();
        Log.e(TAG, "getCurrentTime: " +
                mCurrentHour +":"+mCurrentMinute+":"+mCurrentSecond);
    }

    public void  start(){
        stop();
        handler.postDelayed(runnable, Unit);
    }

    private void stop() {
        if (handler!=null){
            try {
                handler.removeCallbacks(runnable);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public float dip2px(Context context, int dpValue) {
        float density = context.getResources().getDisplayMetrics().density;
        float pxValue = dpValue * density + 0.5f;
        return pxValue;
    }
}
